package cool.mylog.mylog2end.bean;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.Fastjson2TypeHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * Relation表实体类
 */
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "relation", autoResultMap = true)
public class Relation {
    @TableId
    private Long id;

    /**
     * 创建用户的id
     */
    private Long userid;

    /**
     * 创建用户的name
     */
    private String username;

    private String from;

    private String name;

    @TableField(typeHandler = Fastjson2TypeHandler.class)
    private JSONObject info  = new JSONObject();

    /**
     * 根据JSON字符串生成Relation对象
     * JSON中没有的既设置为null
     *
     * @return Relation对象
     */
    public static Relation getRelationByJson(String relationJson) {
        JSONObject relationJsonObj = JSON.parseObject(relationJson);
        return new Relation(
                relationJsonObj.getLong("id"),
                relationJsonObj.getLong("userid"),
                relationJsonObj.getString("username"),
                relationJsonObj.getString("from"),
                relationJsonObj.getString("name"),
                relationJsonObj.getJSONObject("info")
        );
    }

}
